package com.artifact.mit.mypoint.fragment.app;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

import android.content.Context;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.artifact.mit.mypoint.R;
import com.artifact.mit.mypoint.service.model.api.ProductItem;
import com.artifact.mit.mypoint.service.model.api.ProductItem.ProductItemDetail;

public class ProductDetailHtmlFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.product_detail_html, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		WebView webView = (WebView) getView().findViewById(R.id.webView1);
		try {
			ContextWrapper cw = new ContextWrapper(getActivity());
			File directory = cw.getDir("dat", Context.MODE_PRIVATE);
			File file = new File(directory, "product.ser");
			FileInputStream f = new FileInputStream(file);
			ObjectInputStream s = new ObjectInputStream(f);
			ProductItem item = (ProductItem) s.readObject();
			s.close();
			ProductItemDetail itemDetail = item.getProductItemDetails()[getArguments().getInt("index")];
			webView.loadData(
					itemDetail.getName() + "<br/>"
							+ itemDetail.getDescription() + "<br/>"
							+ itemDetail.getSpec(), "text/html", "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
